package cn.com.lms.business.user.web;

import cn.com.lms.business.report.web.vo.CourseLearnReportVo;
import cn.com.lms.business.user.persistence.entity.SysUserVo;
import cn.com.lms.business.user.service.SysUserVoService;
import com.bnzj.core.persistence.query.CompareExpression;
import com.bnzj.core.persistence.query.CompareType;
import com.bnzj.core.persistence.query.IExpression;

import com.bnzj.core.persistence.query.QueryHelpBean;
import com.bnzj.core.rest.ResponseResult;
import com.bnzj.core.webmvc.util.WebMvcUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.hibernate.internal.util.StringHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Tag(name = "系统管理员", description = "系统管理员")
@RestController
@RequestMapping("/system/admin")
public class SysAdminController {

    @Autowired
    SysUserVoService sysUserVoService;


    @GetMapping(value = "/getJobTypeName")
    public ResponseResult<List<String>> getJobTypeName(HttpServletRequest request) {
        List<String> jobNameList = sysUserVoService.findJobTypeName();
        return ResponseResult.success(jobNameList);
    }

    /**
     * @param request
     * @return com.bnzj.core.rest.ResponseResult<org.springframework.data.domain.Page>
     * @depict 分页查询
     * @auth Barry
     * @createDate 2020/2/17
     * @updateAuthOrOther
     */
    @Operation(summary = "分页查询", description = "分页查询", parameters = {
            @Parameter(name = "deptId", description = "部门id", allowEmptyValue = true, allowReserved = true, schema = @Schema(type = "long", description = "部门id")),
            @Parameter(name = "jobTypeNames", description = "岗位", allowEmptyValue = true, allowReserved = true, schema = @Schema(type = "String", description = "岗位，逗号分隔")),
            @Parameter(name = "limit", description = "分页大小", allowEmptyValue = true, allowReserved = true, schema = @Schema(type = "int", description = "分页大小")),
            @Parameter(name = "offset", description = "页码", allowEmptyValue = true, allowReserved = true, schema = @Schema(type = "int", description = "页码")),
            @Parameter(name = "personnel", description = "在职状态(1在职，0离职)", allowEmptyValue = true, allowReserved = true, schema = @Schema(implementation = Integer.class, description = "在职状态(1在职，0离职")),
            @Parameter(name = "type", description = "用户类型A 内部用户B供应商用户", allowEmptyValue = true, allowReserved = true, schema = @Schema(implementation = String.class, description = "用户类型A 内部用户B供应商用户")),
            @Parameter(name = "enabled", description = "默认查启用 值为1 、启用0禁用", allowEmptyValue = true, allowReserved = true, schema = @Schema(implementation = Integer.class, description = "默认查启用 值为1 、启用0禁用")),
            @Parameter(name = "keyWord", description = "根据中英文名/员工编号/手机/邮箱模糊查询", allowEmptyValue = true, allowReserved = true, schema = @Schema(implementation = String.class, description = "根据中英文名/员工编号/手机/邮箱模糊查询")),
    })
    @PostMapping(value = "/page")
    public ResponseResult<Page<SysUserVo>> getUserPage(HttpServletRequest request,
                                                       @io.swagger.v3.oas.annotations.parameters.RequestBody(description = "屏蔽用户ids", content = {@Content(array = @ArraySchema(schema = @Schema(type = "Long", implementation = Long.class, description = "用户id")))})
                                                     @RequestBody(required = false) List<Long> noIds) {
        Pageable pageable = WebMvcUtils.initPage(request, Sort.by(Sort.Direction.DESC, "u.create_time"));

        String querySQL="";
        String jobTypeNames = request.getParameter("jobTypeNames");
        if (StringHelper.isNotEmpty(jobTypeNames)) {
            String [] arr =jobTypeNames.split(",");
            List<String> jobTypeNameList=Arrays.asList(arr);
            StringBuilder sb=new StringBuilder(" and u.job_type_name in (");
            jobTypeNameList.stream().forEach(jobTypeName -> {
                        sb.append("'"+jobTypeName+"'"+",");
                    }

            );
            sb.append("'0')");
            querySQL=sb.toString();

        }
        QueryHelpBean queryHelpBean = SysUserVoService.getQueryHelpBean(querySQL);
        queryHelpBean.setPageable(pageable);
        IExpression deptIdCondition = CompareExpression.build("u.dept_id", CompareType.equal, request.getParameter("deptId"));
        IExpression personnelCondition = CompareExpression.build("u.personnel", CompareType.equal, request.getParameter("personnel"));
        IExpression typeCondition = CompareExpression.build("u.type", CompareType.equal, request.getParameter("type"));
        if (!CollectionUtils.isEmpty(noIds)) {
            IExpression notIdsCondition = CompareExpression.build("u.id", CompareType.notIn, noIds);
            queryHelpBean.addWhere(notIdsCondition);
        }

        IExpression enableCondition = CompareExpression.build("u.enabled", CompareType.equal, request.getParameter("enabled"));
//        IExpression enabledCondition = CompareExpression.build("u.enabled", CompareType.equal, 1);
        String keyWord = request.getParameter("keyWord");
        if(StringHelper.isNotEmpty(keyWord) && ("是".equals(keyWord.trim()) || "否".equals(keyWord.trim()))){
            IExpression isRecordCondition = CompareExpression.build("u.is_record", CompareType.equal, keyWord);
            queryHelpBean.addWhere(isRecordCondition);
            keyWord="";
        }
        // 在职
        IExpression orCondition7 = CompareExpression.build("u.personnel", CompareType.equal, true);
        queryHelpBean.addWhere(orCondition7);


        IExpression orCondition1 = CompareExpression.build("u.cname", CompareType.like, keyWord);
        IExpression orCondition2 = CompareExpression.build("u.account", CompareType.like, keyWord);
        IExpression orCondition4 = CompareExpression.build("u.email", CompareType.like, keyWord);
        IExpression orCondition5 = CompareExpression.build("u.phone", CompareType.like, keyWord);
        IExpression orCondition6 = CompareExpression.build("u.ename", CompareType.like, keyWord);
        IExpression orConditions = orCondition1.or(orCondition2, orCondition4, orCondition5, orCondition6);
//        queryHelpBean.addWhere(personnelCondition, deptIdCondition, enabledCondition, orConditions);
        queryHelpBean.addWhere(personnelCondition, deptIdCondition, orConditions, typeCondition, enableCondition);

        Page<SysUserVo> all1 = sysUserVoService.findAll(queryHelpBean);
        return ResponseResult.success(all1);
    }


}
